package orgd

import (
	"time"

	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"github.com/pkg/errors"
	corgd "gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/orgd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/repo/mgorepo/baser"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

type orgRepo struct {
	baser.BaseRepo
}

// 新增组织
func (r orgRepo) AddOrg(ctx ddd.Context, orgid string, org corgd.OrgDesc) (err error) {
	o, err := r.GetOrg(ctx, orgid)
	if err != nil && err != mgo.ErrNotFound {
		return err
	}
	if o != nil {
		if o.Status == 0 {
			return errors.Errorf("已存在组织展板，不能新创建")
		}
		// 已经删除需要恢复
		update := bson.M{
			"status":     0,
			"icon":       org.Icon,
			"columnids":  org.Columnids,
			"updateTime": time.Now(),
			"updater":    ctx.UID(),
		}
		return r.DBUpdateID(ctx, collNameOrg, orgid, update)

	}
	c := collOrg{
		OrgID:   orgid,
		CreTime: time.Now(),
		Creator: ctx.UID(),
	}
	c.FullDesc(org)
	return r.DBInsert(ctx, c)
}

// 获取指定组织：根据唯一标识获取组织实体
func (r orgRepo) GetOrg(ctx ddd.Context, orgid string) (*corgd.OrgAggregate, error) {
	a, err := r.GetByCollAggregate(ctx, &collOrg{OrgID: orgid})
	if err != nil && err != mgo.ErrNotFound {
		return nil, errors.Wrapf(err, "获取组织信息失败")
	}
	if a == nil {
		return nil, nil
	}
	return a.(*corgd.OrgAggregate), nil
}

// // 分页获取组织列表：根据唯一标识获取下一页指定数量的组织实体集合
// // total 总数量
// QueryPage(ctx ddd.Context, is OrgSearch, pi common.PageInfo) (npi common.PageInfo, result []OrgAggregate, err error)
// 修改组织
func (r orgRepo) UpdateDesc(ctx ddd.Context, orgid string, org corgd.OrgDesc) error {
	// 已经删除需要恢复
	update := bson.M{
		"status":     0,
		"icon":       org.Icon,
		"columnids":  org.Columnids,
		"updateTime": time.Now(),
		"updater":    ctx.UID(),
	}
	if err := r.DBUpdateID(ctx, collNameOrg, orgid, update); err != nil {
		return errors.Wrap(err, "更新组织信息失败")
	}
	r.RefeshCache(ctx, &collOrg{OrgID: orgid})
	return nil
}

// 删除组织 标记删除
func (r orgRepo) Delete(ctx ddd.Context, orgid string) error {
	if err := r.DBUpdateID(ctx, collNameOrg, orgid, bson.M{"status": -1, "deleteTime": time.Now(), "deleter": ctx.UID()}); err != nil {
		return errors.Wrap(err, "删除组织失败")
	}
	return r.DeleteCache(ctx, &collOrg{OrgID: orgid})
}

// 发布展板
func (r orgRepo) Publish(ctx ddd.Context, orgid string) error {
	if err := r.DBUpdateCollAggregate(ctx, &collOrg{OrgID: orgid}, bson.M{"status": 1}); err != nil {
		return errors.Wrap(err, "发布展板失败")
	}
	return nil
}

// 取消发布展板
func (r orgRepo) NoPublish(ctx ddd.Context, orgid string) error {
	if err := r.DBUpdateCollAggregate(ctx, &collOrg{OrgID: orgid}, bson.M{"status": 0}); err != nil {
		return errors.Wrap(err, "取消发布展板失败")
	}
	return nil
}

// 彻底删除指定组织
func (r orgRepo) Destroy(ctx ddd.Context, orgid string) error {
	return r.DBDestroyID(ctx, collNameOrg, orgid)
}

func (r orgRepo) UpdateShowColumn(ctx ddd.Context, orgid string, columnids []string) error {

	// 更新显示栏目
	update := bson.M{
		"columnids":  columnids,
		"updateTime": time.Now(),
		"updater":    ctx.UID(),
	}
	if err := r.DBUpdateID(ctx, collNameOrg, orgid, update); err != nil {
		return errors.Wrap(err, "更新组织信息失败")
	}
	r.RefeshCache(ctx, &collOrg{OrgID: orgid})
	return nil
}
func (r orgRepo) UpdateIcon(ctx ddd.Context, orgid string, icon []string) error {
	// 更新图标
	update := bson.M{
		"icon":       icon,
		"updateTime": time.Now(),
		"updater":    ctx.UID(),
	}
	if err := r.DBUpdateID(ctx, collNameOrg, orgid, update); err != nil {
		return errors.Wrap(err, "更新组织信息失败")
	}
	r.RefeshCache(ctx, &collOrg{OrgID: orgid})
	return nil
}
